home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
usenet
/
sources
/
volume91
/
utilitys
/
sman_1_0
/
part01
/
src
/
sless_1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-05-18
|
7KB
|
405 lines
#include "sless_defs.h"
#include <libraries/sregexpbase.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <exec/execbase.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/sregexp_protos.h>
Prototype void backup(int);
Prototype char * getbackline(int *);
Prototype int displines(char *);
Prototype char * skipdispline(char *);
Prototype void downone(void);
Prototype long skiponeline(long);
Prototype int breakfn(void);
Prototype void bottomline(void);
Prototype int searchfor(void);
Prototype void nextpat(void);
Prototype void prevpat(void);
Prototype void behelpfull(void);
Prototype void chkabort(void);
Prototype void uponeline(void);
extern const char memory[],nolast[],nopat[],wait[],abort[];
extern char seek;
extern long startpos,nextpos;
extern FILE *file,*ofile;
extern BPTR con;
extern int sx,sy;
extern struct SregExp *lastspat;
extern struct ExecBase *SysBase;
void
backup(n)
int n;
{
char *p,*q,*h;
int l = 0;
show_line;
do {
if (l)
free(p);
if (!(p = getbackline(&startpos)))
break;
l += displines(p);
} while (l < n);
h = p;
while (l > n) {
q = skipdispline(p);
startpos += q - p;
p = q;
l-- ;
}
if (h)
free(h);
refreshscreen();
}
char *
getbackline(pos)
long *pos;
{
char *p,c;
int l = 0;
show_line;
if (*pos == 0)
return NULL;
sseekbot(file,*pos-1);
while (*pos > 0) {
c = sgetcur(file);
if (c == '\n' && l != 0) {
sseek(file,+1,SEEK_CUR);
break;
}
sseek(file,-1,SEEK_CUR);
l++;
(*pos)--;
}
if (!(p = malloc(l+1)))
leave(12,memory);
sgets(p,l+1,file);
return p;
}
int
displines(p)
char *p;
{
int l = 1;
show_line;
while (*(p = skipdispline(p))) l++;
return l;
}
char *
skipdispline(p)
char *p;
{
int l = 0;
show_line;
while (*p && l < sx) {
switch (*p++) {
case (12) :
return (l + 2 <= sx) ? p : p - 1;
case ((char)0x1b) :
if (*p != '[')
break;
p++;
case ((char)0x9b) :
while (isdigit(*p) || *p == ';' || *p == ' ')
p++;
p++;
break;
case (9) :
l = (l + 8) & (~7);
if (l > sx)
return p-1;
break;
case ('\n') :
return p;
default :
l++;
}
}
while ((*p == 0x1B && *(p+1) == '[') || *p == (char)0x9b) {
if (*p == 0x1b)
p++;
do {
p++;
} while (isdigit(*p) || *p == ';' || *p == ' ');
p++;
}
if (*p == '\n')
p++;
return p;
}
void
downone()
{
int pos;
show_line;
if (seek) {
pos = stell(file);
startpos = skiponeline(startpos);
sseek(file,pos,SEEK_SET);
}
bottomline();
if (seek)
nextpos = stell(file);
putline();
if (seof(file))
nextpos = -1;
putconstr(": ",con);
}
long
skiponeline(pos)
long pos;
{
char *p,*q;
show_line;
sseek(file,pos,SEEK_SET);
if (!(p = sgetline(file)))
return pos;
q = skipdispline(p);
free(p);
return pos + q - p;
}
void
uponeline()
{
char *p,*h,*q;
int l,j;
long t;
h = p = getbackline(&startpos);
l = displines(p);
while (l > 1) {
q = skipdispline(p);
startpos += q - p;
p = q;
l--;
}
free(h);
if (nextpos < 0) {
sseek(file,startpos,SEEK_SET);
l = 0;
while ((p = sgetline(file)) && (l += displines(p)) < sy - 2) {
free(p);
}
if (p) {
nextpos = stell(file);
if (l > sy - 2) {
j = displines(p);
q = p;
while (l - j < sy - 2) {
q = skipdispline(q);
j--;
}
nextpos -= p + strlen(p) - q;
}
free(p);
} else
nextpos = -1;
} else {
h = p = getbackline(&nextpos);
l = displines(p);
while (l > 1) {
q = skipdispline(p);
nextpos += q - p;
p = q;
l-- ;
}
free(h);
}
sseek(file,startpos,SEEK_SET);
putconstr("\x9b0 p\x9bT\x9b0;0;H",con);
putline();
bottomline();
putconstr(": \x9b p",con);
t = skiponeline(nextpos);
sseek(file,t,SEEK_SET);
}
int
breakfn()
{
show_line;
if (con) {
while (WaitForChar(con,100))
rawreadcon(con);
}
leave(5,"***BREAK***\n");
}
void
bottomline()
{
show_line;
putconchar('\x9b',con);
putconint(sy,con);
putconstr(";0H\x9bK",con);
}
int
searchfor()
{
char *pat,buff[204];
struct SregExp *spat;
show_line;
if (!(pat = getstr(con))) {
bottomline();
putconstr(": ",con);
return FALSE;
}
if (!IsWild(pat)) {
strcpy(buff,"#?");
strcat(buff,pat);
strcat(buff,"#?");
} else {
strcpy(buff,pat);
strcat(buff,"\n");
}
free(pat);
if (!(spat = ParseSregExp(buff)))
return FALSE;
if (lastspat)
FreeSregExp(lastspat);
lastspat = spat;
return TRUE;
}
void
nextpat()
{
long h,i;
char *p;
struct Task *task = SysBase->ThisTask;
show_line;
if (!lastspat) {
bottomline();
putconstr(nolast,con);
return;
}
bottomline();
putconstr(wait,con);
h = startpos;
i = stell(file);
sseek(file,startpos,SEEK_SET);
p = sgetline(file);
free(p);
startpos = stell(file);
while (p = sgetline(file)) {
if (MatchSregExp(p,lastspat,FALSE)) {
free(p);
refreshscreen();
return;
}
free(p);
startpos = stell(file);
if (task->tc_SigRecvd & SIGBREAKF_CTRL_C) {
SetSignal(0, SIGBREAKF_CTRL_C);
bottomline();
putconstr(abort,con);
goto done;
}
}
bottomline();
putconstr(nopat,con);
done:
startpos = h;
sseek(file,i,SEEK_SET);
}
void
prevpat()
{
long h,i;
char *p;
struct Task *task = SysBase->ThisTask;
show_line;
if (!lastspat) {
bottomline();
putconstr(nolast,con);
return;
}
bottomline();
putconstr(wait,con);
h = startpos;
i = stell(file);
while (p = getbackline(&startpos)) {
if (MatchSregExp(p,lastspat,FALSE)) {
free(p);
refreshscreen();
return;
}
free(p);
if (task->tc_SigRecvd & SIGBREAKF_CTRL_C) {
SetSignal(0, SIGBREAKF_CTRL_C);
bottomline();
putconstr(abort,con);
goto done;
}
}
bottomline();
putconstr(nopat,con);
done:
startpos = h;
sseek(file,i,SEEK_SET);
}
void
behelpfull()
{
char oseek;
long ostartpos,onextpos;
show_line;
ofile = file;
oseek = seek;
ostartpos = startpos;
onextpos = nextpos;
seek = TRUE;
file = sopen(NULL);
startpos = 0;
refreshscreen();
waitcommand();
sclose(file);
file = ofile;
ofile = NULL;
seek = oseek;
startpos = ostartpos;
nextpos = onextpos;
refreshscreen();
}
void
chkabort(void)
{
struct Task *task = SysBase->ThisTask;
if (task->tc_SigRecvd & SIGBREAKF_CTRL_C) {
SetSignal(0, SIGBREAKF_CTRL_C);
breakfn();
}
}